<?php

namespace App\Admin\Controllers;

use App\Models\Article;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class ArticleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('文章');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('文章');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('文章');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Article::class, function (Grid $grid) {

            $grid->model()->orderBy('id', 'DESC');
            $grid->id('ID')->sortable();
            $grid->title('标题');
            $grid->adminUser()->username('发布人');
            $grid->status('状态')->switch([
                'on'  => ['value' => 1, 'text' => '可见', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '不可见', 'color' => 'danger'],
            ]);
            $grid->created_at('创建时间');

            $grid->disableExport();

            $grid->filter(function ($filter) {
                $filter->like('title', '标题');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Article::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->hidden('id');
            $form->text('title', '标题');
            $form->switch('status', '状态')->states([
                'on'  => ['value' => 1, 'text' => '可见', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '不可见', 'color' => 'danger'],
            ])->default(1);

            $form->editor('content', '文章内容')->rules('required', [
                'required' => '文章内容不能为空'
            ]);

            $form->saving(function (Form $form) {
                $where = [['title', '=', $form->title]];
                $id = $form->id;
                if (!empty($id)) {
                    $where[] = ['id', '<>', $id];
                }
                $is_exist = Article::where($where)->first();
                if (!empty($is_exist)) {
                    $error = new MessageBag([
                        'title'   => '错误',
                        'message' => '文章标题已存在',
                    ]);

                    return back()->with(compact('error'));
                }
                $form->admin_id = Admin::user()->id;
            });
        });
    }
}
